package com.um.jdy.app.shop.manager;


import cn.hutool.core.collection.ListUtil;
import com.um.jdy.app.shop.entity.dto.GoodsRecordPageDTO;
import com.um.jdy.app.shop.entity.vo.GoodsItemVO;
import com.um.jdy.common.member.service.MemberUserService;
import com.um.jdy.common.shop.service.GoodsService;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.entity.RangeEntity;
import com.um.springboot.starter.utils.condition.ConditionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * @Title
 * @Description
 * @Author renjunwei
 * @Date 2023/5/22 10:37
 */
@Slf4j
@Service
public class GoodsRecordManager {

    @Autowired
    GoodsService goodsService;

    @Autowired
    MemberUserService userService;


    /**
     * 分页查询商品
     * @param dto
     * @return
     */
    public Page<GoodsItemVO> getPage(GoodsRecordPageDTO dto){
        Page page = new Page(dto);

        Map<String, String> condition = ConditionBuilder.create()
                .putLike("g.shop_goods_name", dto.getShop_goods_name())
                .put("mc.member_user_id", userService.get().getMemberUserId())
                .put("mc.com_id",userService.getComId())
                .build();

        RangeEntity rangeEntity = new RangeEntity();
        rangeEntity.setName("mc.create_time");
        rangeEntity.setBeginValue(dto.getStart_time());
        rangeEntity.setBeginValue(dto.getEnd_time());
        List<RangeEntity> rangeList = ListUtil.toList(rangeEntity);

        return goodsService.getCMSViewModelPageBySqlName(page, "get_record_goods_page", "get_record_goods_count",
                GoodsItemVO.class, condition.keySet().toArray(new String[0]), condition.values().toArray(new String[0]),
                rangeList, new String[]{}, new String[]{});
    }

}
